
//
//  YYYEmoticonPackageModel.swift
//  HotNews
//
//  Created by yang on 2017/11/5.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

import UIKit

class YYYEmoticonPackageModel: NSObject {

    //表情包的分组名称
    var groupName: String?
    
    //背景图片名称
    var bgImageName: String?
    
    //表情包的存放目录
    var directory: String?
    {
        didSet
        {
            guard let directory = directory,
                  let path = Bundle.main.path(forResource: "HMEmoticon.bundle", ofType: nil),
                  let bundle = Bundle(path: path),
                  let infoPath = bundle.path(forResource: "info.plist", ofType: nil, inDirectory: directory),
                  let array = NSArray(contentsOfFile: infoPath) as? [[String: String]],
                  let modelsArray = NSArray.yy_modelArray(with: YYYEmoticonModel.self, json: array) as? [YYYEmoticonModel]  else
            {
                return
            }
            for model in modelsArray
            {
                model.directory = directory
            }
            emoticons += modelsArray
        }
    }
    
    //懒加载表情模型的空数组
    //懒加载可以避免后续的解包问题
    lazy var emoticons = [YYYEmoticonModel]()
    
    //每页的表情数量
    var numberOfPages: Int
    {
        return (emoticons.count - 1)/20 + 1
    }
    
    func emoticon(page: Int) -> [YYYEmoticonModel]
    {
        let count = 20
        let location = page * count
        var length = count
        if location + length > emoticons.count
        {
            length = emoticons.count - location
        }
        let range = NSRange(location: location, length: length)
        let subArray = (emoticons as NSArray).subarray(with: range) as! [YYYEmoticonModel]
        return subArray
    }
    
    override var description: String
    {
        return yy_modelDescription()
    }
}
